<?php
declare (strict_types=1);

namespace app\admin\logic\content\sensitive;

use app\admin\model\content\sensitive\Sensitive as SensitiveModel;
use app\common\exception\LogicException;
use think\db\exception\DbException;
use think\facade\Request;
use think\Model;
use think\Paginator;

/**
 * 敏感词逻辑层
 */
class Sensitive extends SensitiveModel
{
    /**
     * 获取列表
     *
     * @param array $map
     * @return Paginator
     * @throws DbException
     */
    public function getList(array $map = []): Paginator
    {
        return $this->where($map)->order('update_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * created
     *
     * @param array $data
     * @return int
     */
    public function created(array $data): int
    {
        $titles = explode("\n", $data['keyword']);
        $insert = [];
        $status = 1; // $data['status'] ?? 1;
        foreach ($titles as $title) {
            if (!$title) {
                continue;
            }
            $insert[] = [
                'mhm_id'      => request()->mhm_id,
                'title'       => $title,
                'status'      => $status,
                'create_time' => Request::time(),
                'update_time' => Request::time(),
            ];
        }

        // 创建成功
        if (!$count = $this->insertAll($insert)) {
            throw new LogicException('创建失败');
        }

        // Log::write('新建敏感词【' . $data['title'] . '】', 'content_sensitive', 'create', true);

        // 返回值
        return $count;
    }

    /**
     * updated
     *
     * @param       $id
     * @param array $data
     * @return Sensitive|array|Model
     */
    public function updated($id, array $data)
    {
        $sensitive = $this->where('id', $id)->findOrFail();
        if (isset($data['title'])) {
            $sensitive->title = $data['title'];
        }

        // if (isset($data['status'])) {
        //     $sensitive->status = intval($data['status']) == 1 ? 1 : 0;
        // }

        if (!$sensitive->save()) {
            throw new LogicException('修改失败');
        }

        // Log::write('编辑敏感词【' . $data['title'] . '】', 'content_sensitive', 'update', true);

        return $sensitive;
    }

    /**
     * updated
     *
     * @param $id
     * @param $field
     * @param $value
     * @return Sensitive|array|Model
     */
    public function updatedBy($id, $field, $value)
    {
        $sensitive = $this->where('id', $id)->findOrFail();

        $sensitive->$field = $value;

        if (!$sensitive->save()) {
            throw new LogicException('修改失败');
        }

        // Log::write('编辑敏感词【' . $data['title'] . '】', 'content_sensitive', 'update', true);

        return $sensitive;
    }

    /**
     * deleted
     *
     * @param $id
     */
    public function deleted($id)
    {
        $sensitive = $this->where('id', $id)->findOrFail();

        if (!$sensitive->delete()) {
            throw new LogicException("删除失败");
        }

        // Log::write('删除敏感词【' . $sensitive->title . '】', 'content_sensitive', 'delete', true);
    }

    /**
     * 批量更新数据
     *
     * @param array  $ids
     * @param string $field
     * @param        $value
     */
    public function batchUpdated(array $ids, string $field, $value)
    {
        $ret = SensitiveModel::update([$field => $value], [['id', 'in', $ids]]);
        if (!$ret) {
            throw new LogicException('批量操作失败');
        }
    }

    /**
     * 批量删除
     *
     * @param $ids
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deletes($ids): void
    {
        $list = $this->whereIn('id', $ids)->select();
        if (!$list->delete()) {
            throw new LogicException('操作失败');
        }
    }

}
